popover: Make it possible to constrain to toplevel
authorMatthias Clasen <mclasen@redhat.com>
Tue, 1 Dec 2015 19:56:56 +0000 (14:56 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 1 Dec 2015 22:26:25 +0000 (17:26 -0500)
Under X11, popovers are always constrained to the toplevel
window. Under Wayland, they aren't. This commit adds a
property that allows to explicitly constrain popovers to
the toplevel, giving them the same behavior under Wayland
as under X11.

https://bugzilla.gnome.org/show_bug.cgi?id=757474

docs/reference/gtk/gtk3-sections.txt
gtk/gtkenums.h
gtk/gtkpopover.c
gtk/gtkpopover.h

index 7a462c9a0bf1073e0111534bdd25d563401d40dc..4f0aaf4540327a1e38c17ff4fc667a404781b526 100644 (file)
@@ -8141,6 +8141,9 @@ gtk_popover_set_pointing_to
 gtk_popover_get_pointing_to
 gtk_popover_set_position
 gtk_popover_get_position
+GtkPopoverConstraint
+gtk_popover_set_constrain_to
+gtk_popover_get_constrain_to
 gtk_popover_set_modal
 gtk_popover_get_modal
 gtk_popover_set_transitions_enabled
index d2ada7e8a623b5ac3190057f40716b701db882a8..73bc316c42df92d07c5c79e918c691ba276060c6 100644 (file)
@@ -1126,4 +1126,23 @@ typedef enum
   GTK_PAN_DIRECTION_DOWN
 } GtkPanDirection;
 
+/**
+ * GtkPopoverConstraint:
+ * @GTK_POPOVER_CONSTRAINT_NONE: Don't constrain the popover position
+ *   beyond what is imposed by the implementation
+ * @GTK_POPOVER_CONSTRAINT_WINDOW: Constrain the popover to the boundaries
+ *   of the window that it is attached to
+ *
+ * Describes constraints to positioning of popovers. More values
+ * may be added to this enumeration in the future.
+ *
+ * Since: 3.20
+ */
+typedef enum
+{
+  GTK_POPOVER_CONSTRAINT_NONE,
+  GTK_POPOVER_CONSTRAINT_WINDOW
+} GtkPopoverConstraint;
+
+
 #endif /* __GTK_ENUMS_H__ */
index 0506c5a76f4adf657bb560d1c6a549d2c85fcb35..d7f32299354f7939cf8e710f563497228c493b45 100644 (file)
@@ -125,7 +125,8 @@ enum {
   PROP_POINTING_TO,
   PROP_POSITION,
   PROP_MODAL,
-  PROP_TRANSITIONS_ENABLED
+  PROP_TRANSITIONS_ENABLED,
+  PROP_CONSTRAIN_TO
 };
 
 enum {
@@ -151,6 +152,7 @@ struct _GtkPopoverPrivate
   GtkAdjustment *vadj;
   GtkAdjustment *hadj;
   GdkRectangle pointing_to;
+  GtkPopoverConstraint constraint;
   guint prev_focus_unmap_id;
   guint hierarchy_changed_id;
   guint size_allocate_id;
@@ -199,6 +201,7 @@ gtk_popover_init (GtkPopover *popover)
   popover->priv->modal = TRUE;
   popover->priv->tick_id = 0;
   popover->priv->transitions_enabled = TRUE;
+  popover->priv->constraint = GTK_POPOVER_CONSTRAINT_WINDOW;
 
   context = gtk_widget_get_style_context (GTK_WIDGET (popover));
   gtk_style_context_add_class (context, GTK_STYLE_CLASS_BACKGROUND);
@@ -232,6 +235,10 @@ gtk_popover_set_property (GObject      *object,
       gtk_popover_set_transitions_enabled (GTK_POPOVER (object),
                                            g_value_get_boolean (value));
       break;
+    case PROP_CONSTRAIN_TO:
+      gtk_popover_set_constrain_to (GTK_POPOVER (object),
+                                    g_value_get_enum (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -262,6 +269,9 @@ gtk_popover_get_property (GObject    *object,
     case PROP_TRANSITIONS_ENABLED:
       g_value_set_boolean (value, priv->transitions_enabled);
       break;
+    case PROP_CONSTRAIN_TO:
+      g_value_set_enum (value, priv->constraint);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -986,7 +996,8 @@ gtk_popover_update_position (GtkPopover *popover)
   overshoot[GTK_POS_RIGHT] = rect.x + rect.width + req.width - window_alloc.width;
 
 #ifdef GDK_WINDOWING_WAYLAND
-  if (GDK_IS_WAYLAND_DISPLAY (gtk_widget_get_display (widget)))
+  if (GDK_IS_WAYLAND_DISPLAY (gtk_widget_get_display (widget)) &&
+      priv->constraint == GTK_POPOVER_CONSTRAINT_NONE)
     {
       priv->final_position = priv->preferred_position;
     }
@@ -1698,6 +1709,20 @@ gtk_popover_class_init (GtkPopoverClass *klass)
                                                          TRUE,
                                                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
 
+  /**
+   * GtkPopover:constrain-to:
+   *
+   * Sets a constraint for the popover position.
+   *
+   * Since: 3.20
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_CONSTRAIN_TO,
+                                   g_param_spec_enum ("constrain-to",
+                                                      P_("Constraint"),
+                                                      P_("Constraint for the popover position"),
+                                                      GTK_TYPE_POPOVER_CONSTRAINT, GTK_POPOVER_CONSTRAINT_WINDOW,
+                                                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
   signals[CLOSED] =
     g_signal_new (I_("closed"),
                   G_TYPE_FROM_CLASS (object_class),
@@ -2482,3 +2507,53 @@ gtk_popover_get_default_widget (GtkPopover *popover)
 
   return priv->default_widget;
 }
+
+/**
+ * gtk_popover_set_constrain_to:
+ * @popover: a #GtkPopover
+ * @constraint: the new constraint
+ *
+ * Sets a constraint for positioning this popover.
+ *
+ * Note that not all platforms support placing popovers freely,
+ * and may already impose constraints.
+ *
+ * Since: 3.20
+ */
+void
+gtk_popover_set_constrain_to (GtkPopover           *popover,
+                              GtkPopoverConstraint  constraint)
+{
+  GtkPopoverPrivate *priv = popover->priv;
+
+  g_return_if_fail (GTK_IS_POPOVER (popover));
+
+  if (priv->constraint == constraint)
+    return;
+
+  priv->constraint = constraint;
+  gtk_popover_update_position (popover);
+
+  g_object_notify (G_OBJECT (popover), "constrain-to");
+}
+
+/**
+ * gtk_popover_get_constrain_to:
+ * @popover: a #GtkPopover
+ *
+ * Returns the constraint for placing this popover.
+ * See gtk_popover_set_constrain_to().
+ *
+ * Returns: the constraint for placing this popover.
+ *
+ * Since: 3.20
+ */
+GtkPopoverConstraint
+gtk_popover_get_constrain_to (GtkPopover *popover)
+{
+  GtkPopoverPrivate *priv = popover->priv;
+
+  g_return_val_if_fail (GTK_IS_POPOVER (popover), GTK_POPOVER_CONSTRAINT_WINDOW);
+
+  return priv->constraint;
+}
index f226bb2d4cc7ffdb1cac26c86ecb4f0bf171f1df..61bcd92703be53f1c315123424b027daf553da91 100644 (file)
@@ -109,6 +109,13 @@ void            gtk_popover_set_default_widget (GtkPopover *popover,
 GDK_AVAILABLE_IN_3_18
 GtkWidget *     gtk_popover_get_default_widget (GtkPopover *popover);
 
+GDK_AVAILABLE_IN_3_20
+void                 gtk_popover_set_constrain_to (GtkPopover           *popover,
+                                                   GtkPopoverConstraint  constraint);
+
+GDK_AVAILABLE_IN_3_20
+GtkPopoverConstraint gtk_popover_get_constrain_to (GtkPopover           *popover);
+
 G_END_DECLS
 
 #endif /* __GTK_POPOVER_H__ */